package com.cxm.weilai.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 验证工具包
 * @author pilo
 */
public class ValidateUtil {

    /**
     * 验证密码（非空格非中文6-15位）
     */
    public static boolean validatePassword(String password) {
        if (StringUtils.isBlank(password) || (password.length() < 6) || (password.length() > 15)) { return false; }
        if (isContainsWhiteSpace(password)) { return false; }
        if (isContainsChinese(password)) { return false; }
        return true;
    }

    /** 是否包含空格 */
    public static boolean isContainsWhiteSpace(String str) {
        if (StringUtils.isBlank(str)) return true;
        int length = str.length();
        for (int index = 0; index < length; index++) {
            char ch = str.charAt(index);
            if (Character.isWhitespace(ch)) { return true; }
        }
        return false;
    }

    /** 是否含有汉字 */
    public static boolean isContainsChinese(String str) {
        if (StringUtils.isBlank(str)) return true;
        char[] chars = str.toCharArray();
        String cnRegx = "[\\u4e00-\\u9fa5]+";
        for (char c : chars) {
            if (String.valueOf(c).matches(cnRegx)) { return true; }
        }
        return false;
    }

    /** 验证邮箱地址是否正确 */
    public static boolean checkEmail(String email) {
        boolean flag = false;
        if (StringUtils.isBlank(email)) return flag;
        try {
            Pattern regex = Pattern.compile("^([a-z0-9A-Z]+[-|\\.\\_]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        } catch (Exception ex) {
            flag = false;
        }
        return flag;
    }

    /** 验证手机号码 */
    public static boolean isMobileNumber(String mobiles) {
        boolean flag = false;
        if (StringUtils.isBlank(mobiles)) return flag;
        try {
            Pattern pattern = Pattern.compile("^((1[3,4,7,8][0-9])|(15[^4,\\D]))\\d{8}$");
            Matcher matcher = pattern.matcher(mobiles);
            flag = matcher.matches();
        } catch (Exception exception) {
            flag = false;
        }
        return flag;

    }

    /** 验证手机号码11位数字 */
    public static boolean isMobileNO2(String mobiles) {
        boolean flag = false;
        if (StringUtils.isBlank(mobiles)) return flag;
        try {
            Pattern pattern = Pattern.compile("^\\d{11}$");
            Matcher matcher = pattern.matcher(mobiles);
            flag = matcher.matches();
        } catch (Exception exception) {
            flag = false;
        }
        return flag;

    }

    /** 是否只包含数字 */
    public static boolean isNum(String number) {
        boolean flag = false;
        if (StringUtils.isBlank(number)) return flag;
        try {
            Pattern pattern = Pattern.compile("^[0-9]*[1-9][0-9]*$");
            Matcher matcher = pattern.matcher(number);
            flag = matcher.matches();
        } catch (Exception exception) {
            flag = false;
        }
        return flag;
    }

    /** 是否只包含数字和字母 */
    public static boolean isNumAndChar(String str) {
        boolean flag = false;
        if (StringUtils.isBlank(str)) return flag;
        try {
            Pattern pattern = Pattern.compile("^[A-Za-z0-9]+$");
            Matcher matcher = pattern.matcher(str);
            flag = matcher.matches();
        } catch (Exception exception) {
            flag = false;
        }
        return flag;
    }

    /** 验证userCode是否合法(4-15位字母、数字或下划线，且不能以下划线开头或结尾) */
    public static boolean isValidUserCode(String userCode) {
        boolean flag = false;
        if (StringUtils.isBlank(userCode)) return flag;
        if (userCode.startsWith("_") || userCode.endsWith("_")) return flag;
        try {
            Pattern pattern = Pattern.compile("[a-zA-Z0-9_]{4,15}$");
            Matcher matcher = pattern.matcher(userCode);
            flag = matcher.matches();
        } catch (Exception exception) {
            flag = false;
        }
        return flag;
    }

    /** 验证传入的合同价格是否合法 */
    public static boolean isValidFreight(String freight) {
        if (StringUtils.isBlank(freight)) return false;
        return Pattern.compile("^\\d{1,7}(\\.\\d{1,2})?$").matcher(freight).find();
    }

    /** 验证传入的合同价格是否合法 */
    public static boolean isValidReceiveMoney(String money) {
        if (StringUtils.isBlank(money)) return false;
        return Pattern.compile("^[0-9]+(.[0-9]{1,3})?$").matcher(money).find();
    }

    /** 验证传入的订单号是否合法 */
    public static boolean isValidOrderId(String orderId) {
        if (StringUtils.isBlank(orderId) || (orderId.length() < 10) || (!NumberUtils.isNumber(orderId))) return false;
        return true;
    }
}
